package com.systop.busafter.bus.controller;
import com.systop.busafter.bus.service.BusService;
import com.systop.commer.Io;
import com.systop.commer.ResponseResult;
import com.systop.commer.pojo.Bus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
public class BusController {
    @Autowired
    private BusService busService;
    //修改
    @RequestMapping(value="/updatabus",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult updatabus(HttpServletRequest request, @RequestParam(value = "bus_photo",required = false) MultipartFile bus_photo,
                             @RequestParam(value = "bus_year",required = false) MultipartFile bus_year,
                             @RequestParam(value = "bus_name")String bus_name,@RequestParam(value = "bus_seat")String bus_seat,
                             @RequestParam(value = "bus_s")String bus_s,
                             @RequestParam(value = "bus_price")String bus_price,@RequestParam(value = "bus_remarks")String bus_remarks,
                             @RequestParam(value = "bus_id")String bus_id
                             )throws IOException{

        Bus bus =  busService.BusId(Integer.parseInt(bus_id));
        if (null==bus_photo && null!=bus_year) {
            bus.setBus_year(Io.img(bus_year,request));
            bus.setBus_name(bus_name);
            bus.setBus_seat(Integer.parseInt(bus_seat));
            bus.setBus_price(Float.parseFloat(bus_price));
            bus.setBus_s(bus_s);
            bus.setBus_remarks(bus_remarks);
            bus.setBus_id(Integer.parseInt(bus_id));

        }else if(null==bus_year && null!=bus_photo){
            bus.setBus_photo(Io.img(bus_photo,request));
            bus.setBus_name(bus_name);
            bus.setBus_seat(Integer.parseInt(bus_seat));
            bus.setBus_price(Float.parseFloat(bus_price));
            bus.setBus_s(bus_s);
            bus.setBus_remarks(bus_remarks);
            bus.setBus_id(Integer.parseInt(bus_id));

        }else if(null==bus_photo && null==bus_year){

            bus.setBus_name(bus_name);
            bus.setBus_seat(Integer.parseInt(bus_seat));
            bus.setBus_price(Float.parseFloat(bus_price));
            bus.setBus_s(bus_s);
            bus.setBus_remarks(bus_remarks);
            bus.setBus_id(Integer.parseInt(bus_id));

        }else if(null!=bus_photo && null!=bus_year){
            bus.setBus_photo(Io.img(bus_photo,request));
            bus.setBus_year(Io.img(bus_year,request));
            bus.setBus_name(bus_name);
            bus.setBus_seat(Integer.parseInt(bus_seat));
            bus.setBus_price(Float.parseFloat(bus_price));
            bus.setBus_s(bus_s);
            bus.setBus_remarks(bus_remarks);
            bus.setBus_id(Integer.parseInt(bus_id));

        }

        int i =  busService.updatabus(bus);
        if (i == 0) {
            return ResponseResult.build(500, "修改失败");
        } else {
            return ResponseResult.build(200, "修改成功", i);
        }
    }
    //添加
    @RequestMapping(value="/insertbus",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult insertbus(HttpServletRequest request, @RequestParam(value = "bus_photo",required = false) MultipartFile bus_photo,
                             @RequestParam(value = "bus_year",required = false) MultipartFile bus_year,
                             @RequestParam(value = "bus_name")String bus_name,@RequestParam(value = "bus_seat")String bus_seat,
                             @RequestParam(value = "bus_s")String bus_s,@RequestParam(value = "bus_driver_id")String bus_driver_id,
                             @RequestParam(value = "bus_price")String bus_price,@RequestParam(value = "bus_remarks")String bus_remarks
                             ) throws IOException {
        Bus bus = new Bus();
        int a= Integer.parseInt(bus_driver_id);
        int b= Integer.parseInt(bus_seat);
        bus.setBus_photo(Io.img(bus_photo,request));
        bus.setBus_year(Io.img(bus_year,request));
        bus.setBus_name(bus_name);
        bus.setBus_seat(b);
        bus.setBus_price(Float.parseFloat(bus_price));
        bus.setBus_driver_id(a);
        bus.setBus_s(bus_s);
        bus.setBus_remarks(bus_remarks);


        int i =  busService.insertbus(bus);
        if (i == 0) {
            return ResponseResult.build(500, "添加失败");
        } else {
            return ResponseResult.build(200, "添加成功", i);
        }
    }

    //查看全部
    @RequestMapping(value="/busListall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult busListall(){
        List<Bus> list =  busService.busListall();
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //通过id查List
    @RequestMapping(value="/busIdlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult busIdlist(@RequestParam(value = "bus_id")int bus_id){
        List<Bus> list =  busService.busIdlist(bus_id);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //通过id查返回实体类
    @RequestMapping(value="/BusId",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult BusId(@RequestParam(value = "bus_id")int bus_id){
       Bus l =  busService.BusId(bus_id);
        if (l == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", l);
        }
    }

    //删除
    @RequestMapping(value="/deletebus",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult deletebus(@RequestParam(value = "bus_id")int bus_id){
       int i =  busService.deletebus(bus_id);
        if (i == 0) {
            return ResponseResult.build(500, "删除失败");
        } else {
            return ResponseResult.build(200, "删除成功", i);
        }
    }
    //通过车主id查看
    @RequestMapping(value="/driverIdall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult driverIdall(@RequestParam(value = "bus_driver_id")int bus_driver_id){
        List<Bus> list =  busService.driverIdall(bus_driver_id);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //通过状态查看bus
    @RequestMapping(value="/busstateall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult busstateall(){
        int bus_state=1;
        List<Bus> list =  busService.busstateall(bus_state);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //用价格查看bus
    @RequestMapping(value="/buspriceall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult buspriceall(@RequestParam(value = "bus_price")float bus_price){
        List<Bus> list =  busService.buspriceall(bus_price);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    // 用优惠卷查查看bus
    @RequestMapping(value="/buscouponall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult buscouponall(@RequestParam(value = "bus_coupon")float bus_coupon){
        List<Bus> list =  busService.buscouponall(bus_coupon);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //通过审核(车主需要上传车辆的信息，管理员来审核车辆是否合格)
    @RequestMapping(value="/bustureT",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult bustureT(@RequestParam(value = "bus_id")int bus_id){
           Bus bus = busService.BusId(bus_id);
           if (bus.getBus_state()==0){
               bus.setBus_state(1);
           }else{
               bus.setBus_state(0);
           }
          int i = busService.updatabus(bus);
        if (i == 0) {
            return ResponseResult.build(500, "审核失败");
        } else {
            return ResponseResult.build(200, "审核成功", bus);
        }
    }

    //用车主账号查看bus
    @RequestMapping(value="/busdrivername",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult busdrivername(@RequestParam(value = "bus_s")String bus_s){
        List<Bus> list =  busService.busdrivername(bus_s);
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }


}
